package org.longteng.mobileluck;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

/**
 * 监控显示出所有接口
 */
@Slf4j
@Component
public class ApplicationContextMonitor implements ApplicationContextAware, ApplicationRunner, WebMvcConfigurer {
    private ApplicationContext applicationContext;

    RequestMappingHandlerMapping handlerMapping;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        if (handlerMapping == null) {
            log.warn("handlerMapping为null,无法监控系统发布的api。");
         } else {
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
            log.info("系统发布的api：{}", handlerMethods);

        }
    }

    /**
    //解决 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    //spring.jackson.time-zone=GMT+8 不生效的功能
     #参数意义：
     #JsonInclude.Include.ALWAYS       默认
     #JsonInclude.Include.NON_DEFAULT   属性为默认值不序列化
     #JsonInclude.Include.NON_EMPTY     属性为 空（””） 或者为 NULL 都不序列化
     #JsonInclude.Include.NON_NULL      属性为NULL  不序列化
     WebMvcConfigurer //实现这个接口不用写任务内容就可让 spring.jackson.date-format生效
     Override extendMessageConverters
    // 不用全局侧需求每个返回vo定义注解
    <em>
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date pickUpTime;
    </em>
    */

}
